import * as crypto from 'crypto'


/**
 * 处理加密函数
 * @param plainTextPassword 明文
 * @param encryptionKey 秘钥
 * @param iv 初始向量
 * @returns IV和密文
 */
export const encryptAES256CBC = (plainTextPassword, encryptionKey, iv = crypto.randomBytes(16)) => {
  const cipher = crypto.createCipheriv('aes-256-cbc', encryptionKey, iv)
  cipher.setAutoPadding(true)
  const encryptedData = cipher.update(plainTextPassword, 'utf8', 'base64')
  const finalData = cipher.final('base64')
  const encryptedPassword = `${encryptedData}${finalData}${iv.toString('base64')}`
  const encodedPassword = Buffer.from(encryptedPassword).toString('base64')
  return { encodedPassword, iv }
}
